// pages/userIdent/userIdent.js

/* 用户认证界面 */

var wxNetwork = require("../../utils/network.js");
var wxURL = require("../../utils/requestURL.js");
var feedbackApi = require('../../common/showToast/showToast.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: ['通过编号绑定', '通过手机号绑定'],
    currentTab: 0,

    userNum: '',
    userName: '',
    userAddress: '',
    userPhone:'',
    userTypeWithID:'',
    userTypeWithTel: ''
  },
  
  /** 点击切换 */
  navbarTap: function (e) {
    var that = this;
    that.setData({
      currentTab: e.currentTarget.dataset.idx
    })
  },
 
 /** 编号绑定页面 */
  /** 编号输入 */
  idInput: function (e) {
    var that = this;
    that.data.userNum = e.detail.value;
  },

  /** 姓名输入 */
  nameInput: function (e) {
    var that = this;
    that.data.userName = e.detail.value;
  },

  /** 地址输入 */
  addressInput: function (e) {
    var that = this;
    that.data.userAddress = e.detail.value;
  },
 
  /** 家庭类型 */
  FamilyInput:function(e){
    var that = this;
    if (that.data.currentTab == 0)
    {
      that.data.userTypeWithID = e.detail.value;
    }
    else
    {
      that.data.userTypeWithTel = e.detail.value;
    }
  },

  /** 手机号输入 */
  phoneInput: function (e) {
    var that = this;
    that.data.userPhone = e.detail.value;
  },


  /** 认证按钮 */
  cilckButton: function () {
   
    var that = this;

    if (that.data.currentTab == 0)
    {
      if (that.data.userNum.length == 0)
      {
        feedbackApi.showToast({
          title: '请填写用户编号',
          icon: '/images/others/tishi_cuo.png'
        })
      }
      else{
        var params = {
          'waterUserNum': that.data.userNum,
          'waterUserName': that.data.userName,
          'waterUserAddress': that.data.userAddress,
          'waterUserType': that.data.userTypeWithID,
          'waterUserPhone': '',
          'isAdd': '0',
          'authMethod': '0'
        };

        that.requestForUsers(params);
      }
    }
    else{

      var regExp = /^[1][3,4,5,7,8][0-9]{9}$/;
      if (that.data.userPhone.length == 0) {
        feedbackApi.showToast({
          title: '请填写手机号',
          icon: '/images/others/tishi_cuo.png'
        })
      }
      else if (!regExp.test(that.data.userPhone)){
        feedbackApi.showToast({
          title: '手机号格式错误',
          icon: '/images/others/tishi_cuo.png'
        })
      }
      else {
        var params = {
          'waterUserNum': '',
          'waterUserName': '',
          'waterUserAddress': '',
          'waterUserType': that.data.userTypeWithTel,
          'waterUserPhone': that.data.userPhone,
          'isAdd': '0',
          'authMethod': '1'
        };
        that.requestForUsers(params);
      }
    }
  },

  requestForUsers:function(data){

    var header = {
      'Access-Token': wx.getStorageSync('token'),
      'content-type': 'application/x-www-form-urlencoded'
    };
    
    wxNetwork.postRequest(wxURL.post_addUsers_URL, data, header, function (dict) {
      feedbackApi.showToast({
        title: dict.message.notice,
        icon: '/images/others/tishi_dui.png'
      })

      wx.switchTab({
        url: '../home/home',
      })
    },
      function (dict) {
        wx.showModal({
          title: dict.message.notice,
          content: dict.message.cause,
        })

      },
      function (dict) {

      })
  }
})